import logger from '@common/logger';
import { DELIVERY_TYPE } from '@constants/shop';
import { getBrandNameCookie } from './info';
/**
 * 订单详情转换为review数据
 * @param {*} detail
 * @param {*} skuInfo
 * @param {*} warehouseInfo
 */
export const detailToReview = (detail, skuInfo, storeInfo) => {
    logger.debugger('Detail => 处理直接购买数据', detail, skuInfo, storeInfo);
    const _id = storeInfo ? storeInfo.id : 0;
    const selectItem = {
        isSelect: false,
        maxQuantity: skuInfo.storage,
        pic: detail.title_pics[0] || '',
        price: skuInfo.price,
        prod_id: detail.id,
        prod_name: detail.name,
        quantity: skuInfo.quantity,
        sku_id: skuInfo.id || 0,
        sku_name: skuInfo.name,
        status: detail.status,
        store_id: _id,
        unit: detail.unit,
        weight: skuInfo.weight,
    };
    return [{
        id: storeInfo ? storeInfo.id : 0,
        name: storeInfo ? storeInfo.name : getBrandNameCookie(),
        list: [selectItem],
        couponList: [],
        useCoupon: [],
        discountInfo: {},
        remark: '',
        warehouseInfo: {
            city_regions: [{ start_fee: 0, express_fee: 0 }],
            ordinary_express_config: { first: 0, additional: 0 },
        },
        deliveryPrice: 0,
        totalPrice: 0,
        totalDiscountPrice: 0,
    }];
};

/**
 * 处理仓库信息为可用信息
 * @param {Object} info 仓库信息
 */
const dealOrderItemWarehouseInfo = (info, isBrandMode) => {
    const isCity = info.city_express && info.city_regions.length > 0;
    const deliveryType = info.ordinary_express ? DELIVERY_TYPE.EXPRESS : (isCity ? DELIVERY_TYPE.CITY_EXPRESS : (info.pick_up ? DELIVERY_TYPE.SELF_PICK_UP : ''));
    return {
        warehouseName: info.name,
        warehouseId: isBrandMode ? info.id : info.warehouse_id || info.id,
        isCity: info.city_express, // 是否支持同城
        isCityBeyond: info.city_express && info.city_regions.length === 0, // 是否是超出配送范围
        isExpress: info.ordinary_express, // 是否支持快递
        isPickUp: info.pick_up, // 是否支持自提
        deliveryType, // 配送方式
        timeDays: 0, // 自提、当日达时间
    };
};


/**
 * 获取商品的规格列表
 * @param {Object} goodsDetail 商品详情
 */
export const getGoodsProdsList = (goodsDetail) => {
    const prods = [];
    const skuList = goodsDetail.skus;
    for (let i = 0; i < skuList.length; i++) {
        prods.push({
            id: goodsDetail.id,
            sku_id: skuList[i].id,
        });
    }
    return prods;
};

// 获取默认选中的规格
// 存在库存、价格最低
export const getDefaultSku = (goodsInfo) => {
    const { skus } = goodsInfo;
    const validSkus = skus;
    logger.debugger('当前的Skus为：', skus);
    if (validSkus.length === 0) {
        return null;
    }
    let minPriceItem = validSkus[0];
    for (let i = 1; i < validSkus.length; i++) {
        const item = skus[i];
        if (item.price < minPriceItem.price) {
            minPriceItem = item;
        }
    }
    logger.debugger('默认sku为：', minPriceItem);
    return minPriceItem;
};
